package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class AdminActivity extends AppCompatActivity {
    private final String Tag = "AdminActivity";

    //查询、申请界面的控件
    private Spinner spinner_time;
    private Spinner spinner_name_building;
    private Spinner spinner_crid;
    private Spinner spinner_classtime;
    private Button button_search;
    private Button button_apply;
    private Button button_free;
    private ListView listView;
    private TextView time_applied;
    private TextView user_sa_textview1;
    private TextView user_sa_textview2;

    private String date_selected;//下拉框中选定的时间
    private String name_building_selected;//下拉框中选定的教学楼
    private String classtime_selected;//下拉框中选定的课时
    private String crid_selected;//下拉框中选定的教室号

    ArrayList<String> id_cr_all = new ArrayList<>();//给教室号下拉框的输入值

    //接收LoginActivity传过来的个人信息
    private user_info info = new user_info("id","pwd","name","character");

    /*
    从数据库中取出教室的数据
     */
    private ArrayList<classroom_info> db_classroom_1 = new ArrayList<>();//今天的教室数据
    private ArrayList<classroom_info> db_classroom_2 = new ArrayList<>();//明天的教室数据
    private ArrayList<classroom_info> db_classroom_3 = new ArrayList<>();//后天的教室数据
    /*
    本地的教室数据
     */
    private ArrayList<classroom_info> local_classroom = new ArrayList<>();//今天的教室数据


    /*
    本地教室数据对应的适配器
     */
    ClassroomInfo_Adapter myAdapter;

    /*
    初始化数据库
    */
    private SQLiteDatabase db = MyDataBaseHelper.getDatabase();


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setSA();


        Intent intent = getIntent();
        info = (user_info) intent.getSerializableExtra("user_info");
        if(info != null){
            Toast.makeText(AdminActivity.this,"成功接受数据",Toast.LENGTH_SHORT).show();
        }else{
            //若无信息，则设置默认值
            info = new user_info();
            info.setId("用户编号");
            info.setPwd("密码");
            info.setName("姓名");
            info.setCharacter("身份");
        }
        /*
        取教室表数据
         */
        try{

            //从数据库中取出教室表的数据
            db_classroom_1 = get_db_classroom_infos(db,"classroom1","name_building");
            db_classroom_2 = get_db_classroom_infos(db,"classroom2","name_building");
            db_classroom_3 = get_db_classroom_infos(db,"classroom3","name_building");

            //初始的本地数据为数据库中第一天数据
            local_classroom = db_classroom_1;

        }catch (Exception e){
            e.printStackTrace();
            Log.d(Tag,"从数据库中取教室表时出错，来自onCreate");
        }
        myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,local_classroom,info.getName());

    }

    /*
        查询某个表中所有行，用于简化查询
        */
    private Cursor myQuery(SQLiteDatabase database, String table, String order_by){
        try{
            return database.query(table,null,null,null,null,null,order_by);
        }catch (Exception e){
            e.printStackTrace();
            Log.d("StudentActivity","查询某个表时出错，来自函数myQuery");
            return null;
        }
    }


    /*
    从数据库中取出教室数据
    */
    private ArrayList<classroom_info> get_db_classroom_infos(SQLiteDatabase database,String table,String order_by){
        ArrayList<classroom_info> infos = new ArrayList<classroom_info>();
        Cursor cursor = myQuery(database,table,order_by);
        if(cursor.moveToFirst()){
            do{
                classroom_info info = new classroom_info(
                        cursor.getString(cursor.getColumnIndexOrThrow("name_building")).toString().trim(),
                        cursor.getString(cursor.getColumnIndexOrThrow("id_classroom")).toString().trim(),
                        cursor.getString(cursor.getColumnIndexOrThrow("class1")).toString().trim(),
                        cursor.getString(cursor.getColumnIndexOrThrow("class2")).toString().trim(),
                        cursor.getString(cursor.getColumnIndexOrThrow("class3")).toString().trim(),
                        cursor.getString(cursor.getColumnIndexOrThrow("class4")).toString().trim()
                );
                infos.add(info);
            }
            while(cursor.moveToNext());
        }

        return infos;
    }

    private void setCenter(){

        setContentView(R.layout.user_center);

        TextView user_center_character = findViewById(R.id.user_center_character);
        TextView user_id = findViewById(R.id.user_center_id);
        TextView user_name = findViewById(R.id.user_center_name);
        TextView user_pwd = findViewById(R.id.user_center_pwd);
        TextView user_viewpwd = findViewById(R.id.user_center_viewpwd);
        Button user_logout = findViewById(R.id.user_center_logout);

        //在页面上显示用户的个人信息
        user_center_character.setText("身份："+info.getCharacter());
        user_id.setText("用户编号："+info.getId());
        user_name.setText("用户名："+info.getName().trim());
        user_pwd.setText(info.getPwd());
        user_viewpwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(user_pwd.getVisibility() == View.INVISIBLE){
                    user_pwd.setVisibility(View.VISIBLE);
                }else{
                    user_pwd.setVisibility(View.INVISIBLE);
                }
            }
        });
        user_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db.close();
                finish();
            }
        });
    }

    private void setSA(){

        setContentView(R.layout.user_sa);
        spinner_time = findViewById(R.id.user_sa_spinner_time);
        spinner_name_building= findViewById(R.id.user_sa_spinner_name_building);
        spinner_crid = findViewById(R.id.user_sa_spinner_crid);
        spinner_classtime= findViewById(R.id.user_sa_spinner_classtime);
        button_free = findViewById(R.id.admin_free_button);
        button_search = findViewById(R.id.user_sa_button_search);
        button_apply = findViewById(R.id.user_sa_button_apply);
        listView = findViewById(R.id.user_sa_listview);
        time_applied = findViewById(R.id.user_sa_time_applied);
        user_sa_textview1 = findViewById(R.id.user_sa_textview1);
        user_sa_textview2 = findViewById(R.id.user_sa_textview2);





        local_classroom = db_classroom_1;
        myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,local_classroom,info.getName());
        listView.setAdapter(myAdapter);
        //获取用户选择的时间
        spinner_time.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                date_selected = AdminActivity.this.getResources().getStringArray(R.array.time_selected)[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        }
        );
        //获取用户选择的教学楼
        spinner_name_building.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                name_building_selected = AdminActivity.this.getResources().getStringArray(R.array.name_building)[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        //获得用户申请的教室crid_selected
        spinner_crid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                crid_selected = id_cr_all.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        //获得用户申请的课时classtime_selected
        spinner_classtime.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                classtime_selected = AdminActivity.this.getResources().getStringArray(R.array.class_time)[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        /*
        查询按钮的逻辑
         */
        button_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                id_cr_all.clear();
                Log.d(Tag,"查询按钮被按下");

                if(name_building_selected.equals("全部")){
                    user_sa_textview1.setVisibility(View.INVISIBLE);
                    user_sa_textview2.setVisibility(View.INVISIBLE);
                    spinner_crid.setVisibility(View.INVISIBLE);
                    spinner_classtime.setVisibility(View.INVISIBLE);
                    button_apply.setVisibility(View.INVISIBLE);
                    button_free.setVisibility(View.INVISIBLE);
                }else{
                    user_sa_textview1.setVisibility(View.VISIBLE);
                    user_sa_textview2.setVisibility(View.VISIBLE);
                    spinner_crid.setVisibility(View.VISIBLE);
                    spinner_classtime.setVisibility(View.VISIBLE);
                    button_apply.setVisibility(View.VISIBLE);
                    button_apply.setText("禁止申请");
                    button_free.setVisibility(View.VISIBLE);
                }

                try{
                    /*
                根据选择的时间调整myAdapter
                 */
                    //今天
                    if(date_selected.equals("今天")){
                        if(name_building_selected.equals("全部")){
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_1){
                                if(true){
                                    Log.d(Tag,"查询到教学楼数据，插入中");
                                    tmp.add(info_tmp);
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        } else if (name_building_selected.equals("荔枝楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_1){
                                if(info_tmp.getName_building().equals("荔枝楼")){
                                    Log.d(Tag,"查询到荔枝楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);

                        } else if (name_building_selected.equals("德寄楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_1){
                                if(info_tmp.getName_building().equals("德寄楼")){
                                    Log.d(Tag,"查询到德寄楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        } else if (name_building_selected.equals("学会楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_1){
                                if(info_tmp.getName_building().equals("学会楼")){
                                    Log.d(Tag,"查询到学会楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        }else if (name_building_selected.equals("环宫楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_1){
                                if(info_tmp.getName_building().equals("环宫楼")){
                                    Log.d(Tag,"查询到环宫楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        }else{
                            Toast.makeText(AdminActivity.this,"不存在的楼名，你是怎么做到的",Toast.LENGTH_SHORT);
                        }
                    }
                    //明天
                    else if (date_selected.equals("明天")) {
                        if(name_building_selected.equals("全部")){
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_2){
                                if(true){
                                    Log.d(Tag,"查询到教学楼数据，插入中");
                                    tmp.add(info_tmp);
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        } else if (name_building_selected.equals("荔枝楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_2){
                                if(info_tmp.getName_building().equals("荔枝楼")){
                                    Log.d(Tag,"查询到荔枝楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);

                        } else if (name_building_selected.equals("德寄楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_2){
                                if(info_tmp.getName_building().equals("德寄楼")){
                                    Log.d(Tag,"查询到德寄楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        } else if (name_building_selected.equals("学会楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_2){
                                if(info_tmp.getName_building().equals("学会楼")){
                                    Log.d(Tag,"查询到学会楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        }else if (name_building_selected.equals("环宫楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_2){
                                if(info_tmp.getName_building().equals("环宫楼")){
                                    Log.d(Tag,"查询到环宫楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        }else{
                            Toast.makeText(AdminActivity.this,"不存在的楼名，你是怎么做到的",Toast.LENGTH_SHORT);
                        }
                    }
                    //后天
                    else if(date_selected.equals("后天")){
                        if(name_building_selected.equals("全部")){
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_3){
                                if(true){
                                    Log.d(Tag,"查询到教学楼数据，插入中");
                                    tmp.add(info_tmp);
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        } else if (name_building_selected.equals("荔枝楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_3){
                                if(info_tmp.getName_building().equals("荔枝楼")){
                                    Log.d(Tag,"查询到荔枝楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);

                        } else if (name_building_selected.equals("德寄楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_3){
                                if(info_tmp.getName_building().equals("德寄楼")){
                                    Log.d(Tag,"查询到德寄楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        } else if (name_building_selected.equals("学会楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_3){
                                if(info_tmp.getName_building().equals("学会楼")){
                                    Log.d(Tag,"查询到学会楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        }else if (name_building_selected.equals("环宫楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_3){
                                if(info_tmp.getName_building().equals("环宫楼")){
                                    Log.d(Tag,"查询到环宫楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(AdminActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        }else{
                            Toast.makeText(AdminActivity.this,"不存在的楼名，你是怎么做到的",Toast.LENGTH_SHORT);
                        }
                    }else{
                        Toast.makeText(AdminActivity.this,"不存在的时间，你是怎么做到的",Toast.LENGTH_SHORT);
                    }

                    //更新申请时间


                    ArrayAdapter<String> adapter = new ArrayAdapter<>(AdminActivity.this, android.R.layout.simple_expandable_list_item_1,id_cr_all);
                    spinner_crid.setAdapter(adapter);


                }catch (Exception e){
                    e.printStackTrace();
                    Log.d(Tag,"查询按钮逻辑出错");
                }
                Log.d(Tag,"查询按钮逻辑结束");

            }



        });

        //申请按钮的逻辑
        button_apply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(date_selected.equals("今天")){
                    String query_class_tmp = "class"+classtime_selected;//用于指定查询哪一列数据，如class1
                    try {
                        Cursor cursor = db.query("classroom1",null,"name_building=? and id_classroom=?",new String[]{name_building_selected,crid_selected},null,null,null);
                        if(cursor.moveToFirst()){
                            ContentValues values = new ContentValues();
                            values.put(query_class_tmp,"禁止申请");
                            db.update("classroom1",values,"name_building=? and id_classroom=? ",new String[]{name_building_selected,crid_selected});
                            Log.d(Tag,"596行，数据库数据修改成功");
                            //修改本地数据
                            db_classroom_1 = get_db_classroom_infos(db,"classroom1","name_building");
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        Log.d(Tag,"申请按钮逻辑出错");
                    }
                    Toast.makeText(AdminActivity.this,"权限更改成功",Toast.LENGTH_SHORT).show();
                } else if (date_selected.equals("明天")) {
                    String query_class_tmp = "class"+classtime_selected;//用于指定查询哪一列数据，如class1
                    try {
                        Cursor cursor = db.query("classroom1",null,"name_building=? and id_classroom=?",new String[]{name_building_selected,crid_selected},null,null,null);
                        if(cursor.moveToFirst()){
                            ContentValues values = new ContentValues();
                            values.put(query_class_tmp,"禁止申请");
                            db.update("classroom1",values,"name_building=? and id_classroom=? ",new String[]{name_building_selected,crid_selected});
                            Log.d(Tag,"596行，数据库数据修改成功");
                            //修改本地数据
                            db_classroom_1 = get_db_classroom_infos(db,"classroom1","name_building");
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        Log.d(Tag,"申请按钮逻辑出错");
                    }
                    Toast.makeText(AdminActivity.this,"权限更改成功",Toast.LENGTH_SHORT).show();
                } else if (date_selected.equals("后天")) {
                    String query_class_tmp = "class"+classtime_selected;//用于指定查询哪一列数据，如class1
                    try {
                        Cursor cursor = db.query("classroom1",null,"name_building=? and id_classroom=?",new String[]{name_building_selected,crid_selected},null,null,null);
                        if(cursor.moveToFirst()){
                            ContentValues values = new ContentValues();
                            values.put(query_class_tmp,"禁止申请");
                            db.update("classroom1",values,"name_building=? and id_classroom=? ",new String[]{name_building_selected,crid_selected});
                            Log.d(Tag,"596行，数据库数据修改成功");
                            //修改本地数据
                            db_classroom_1 = get_db_classroom_infos(db,"classroom1","name_building");
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        Log.d(Tag,"申请按钮逻辑出错");
                    }
                    Toast.makeText(AdminActivity.this,"权限更改成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(AdminActivity.this,"不存在的申请日期，你是怎么做到的",Toast.LENGTH_SHORT).show();
                }
            }
        });

        button_free.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(date_selected.equals("今天")){
                    String query_class_tmp = "class"+classtime_selected;//用于指定查询哪一列数据，如class1
                    try {
                        Cursor cursor = db.query("classroom1",null,"name_building=? and id_classroom=?",new String[]{name_building_selected,crid_selected},null,null,null);
                        if(cursor.moveToFirst()){
                            ContentValues values = new ContentValues();
                            values.put(query_class_tmp,"空闲");
                            db.update("classroom1",values,"name_building=? and id_classroom=? ",new String[]{name_building_selected,crid_selected});
                            Log.d(Tag,"596行，数据库数据修改成功");
                            //修改本地数据
                            db_classroom_1 = get_db_classroom_infos(db,"classroom1","name_building");
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        Log.d(Tag,"申请按钮逻辑出错");
                    }
                    Toast.makeText(AdminActivity.this,"权限更改成功",Toast.LENGTH_SHORT).show();
                } else if (date_selected.equals("明天")) {
                    String query_class_tmp = "class"+classtime_selected;//用于指定查询哪一列数据，如class1
                    try {
                        Cursor cursor = db.query("classroom1",null,"name_building=? and id_classroom=?",new String[]{name_building_selected,crid_selected},null,null,null);
                        if(cursor.moveToFirst()){
                            ContentValues values = new ContentValues();
                            values.put(query_class_tmp,"空闲");
                            db.update("classroom1",values,"name_building=? and id_classroom=? ",new String[]{name_building_selected,crid_selected});
                            Log.d(Tag,"687行，数据库数据修改成功");
                            //修改本地数据
                            db_classroom_1 = get_db_classroom_infos(db,"classroom1","name_building");
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        Log.d(Tag,"申请按钮逻辑出错");
                    }
                    Toast.makeText(AdminActivity.this,"权限更改成功",Toast.LENGTH_SHORT).show();
                } else if (date_selected.equals("后天")) {
                    String query_class_tmp = "class"+classtime_selected;//用于指定查询哪一列数据，如class1
                    try {
                        Cursor cursor = db.query("classroom1",null,"name_building=? and id_classroom=?",new String[]{name_building_selected,crid_selected},null,null,null);
                        if(cursor.moveToFirst()){
                            ContentValues values = new ContentValues();
                            values.put(query_class_tmp,"空闲");
                            db.update("classroom1",values,"name_building=? and id_classroom=? ",new String[]{name_building_selected,crid_selected});
                            Log.d(Tag,"596行，数据库数据修改成功");
                            //修改本地数据
                            db_classroom_1 = get_db_classroom_infos(db,"classroom1","name_building");
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        Log.d(Tag,"解除占用按钮逻辑出错");
                    }
                    Toast.makeText(AdminActivity.this,"权限更改成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(AdminActivity.this,"不存在的申请日期，你是怎么做到的",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void setAMD(){
        setContentView(R.layout.admin_add_modify_delete_layout);

        //添加功能的各控件
        EditText admin_add_eT_name_building = findViewById(R.id.admin_add_eT_name_building);
        EditText admin_add_eT_id_classroom = findViewById(R.id.admin_add_eT_id_classroom);
        Button admin_add_button = findViewById(R.id.admin_add_button);
        //清空
        admin_add_eT_name_building.setText("");
        admin_add_eT_id_classroom.setText("");

        //添加按钮的逻辑
        admin_add_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入
                String add_new_name_building = admin_add_eT_name_building.getText().toString().trim();
                String add_new_id_classroom = admin_add_eT_id_classroom.getText().toString().trim();

                if(add_new_id_classroom.equals("") || add_new_name_building.equals("")){
                    Toast.makeText(AdminActivity.this,"输入不可为空",Toast.LENGTH_SHORT).show();
                }else{
                    try {
                        db.beginTransaction();

                        //向表classroom1插入。所有课时的初始值都为“空闲”，下同
                        String sql_1 = "insert into classroom1(name_building,id_classroom,class1,class2,class3,class4) " +
                                "values (?,?,?,?,?,?)";
                        SQLiteStatement stat_1 = db.compileStatement(sql_1);
                        stat_1.bindString(1,add_new_name_building);
                        stat_1.bindString(2,add_new_id_classroom);
                        stat_1.bindString(3,"空闲");
                        stat_1.bindString(4,"空闲");
                        stat_1.bindString(5,"空闲");
                        stat_1.bindString(6,"空闲");
                        stat_1.executeInsert();

                        //向表classroom2插入
                        String sql_2 = "insert into classroom2(name_building,id_classroom,class1,class2,class3,class4) " +
                                "values (?,?,?,?,?,?)";
                        SQLiteStatement stat_2 = db.compileStatement(sql_2);
                        stat_2.bindString(1,add_new_name_building);
                        stat_2.bindString(2,add_new_id_classroom);
                        stat_2.bindString(3,"空闲");
                        stat_2.bindString(4,"空闲");
                        stat_2.bindString(5,"空闲");
                        stat_2.bindString(6,"空闲");
                        stat_2.executeInsert();

                        //向表classroom3插入
                        String sql_3 = "insert into classroom3(name_building,id_classroom,class1,class2,class3,class4) " +
                                "values (?,?,?,?,?,?)";
                        SQLiteStatement stat_3 = db.compileStatement(sql_3);
                        stat_3.bindString(1,add_new_name_building);
                        stat_3.bindString(2,add_new_id_classroom);
                        stat_3.bindString(3,"空闲");
                        stat_3.bindString(4,"空闲");
                        stat_3.bindString(5,"空闲");
                        stat_3.bindString(6,"空闲");
                        stat_3.executeInsert();


                        stat_1.close();
                        stat_2.close();
                        stat_3.close();
                        db.setTransactionSuccessful();
                        db.endTransaction();

                        //更新数据
                        db_classroom_1 = get_db_classroom_infos(db,"classroom1","name_building");
                        db_classroom_2 = get_db_classroom_infos(db,"classroom2","name_building");
                        db_classroom_3 = get_db_classroom_infos(db,"classroom3","name_building");
                        Toast.makeText(AdminActivity.this,"添加教室成功",Toast.LENGTH_SHORT).show();
                    }catch (Exception e){
                        e.printStackTrace();
                        Log.d(Tag,"709行，添加新教室出现问题");
                    }
                }
            }
        });


        //删除功能的各控件
        EditText admin_delete_eT_name_building = findViewById(R.id.admin_delete_eT_name_building);
        EditText admin_delete_eT_id_classroom = findViewById(R.id.admin_delete_eT_id_classroom);
        //清空
        admin_delete_eT_name_building.setText("");
        admin_delete_eT_id_classroom.setText("");

        Button admin_delete_button = findViewById(R.id.admin_delete_button);

        //删除按钮的逻辑
        admin_delete_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String delete_name_building = admin_delete_eT_name_building.getText().toString().trim();
                String delete_id_classroom = admin_delete_eT_id_classroom.getText().toString().trim();

                if(delete_name_building.equals("") || delete_id_classroom.equals("")){
                    Log.d(Tag,"delete_name_building:"+delete_name_building);
                    Log.d(Tag,"delete_name_building:"+delete_id_classroom);
                    Toast.makeText(AdminActivity.this,"输入不可为空",Toast.LENGTH_SHORT).show();
                }else{
                    try {
                        db.beginTransaction();

                        //删除表classroom1中对应数据
                        String sql_1 = "delete from classroom1 " +
                                "where name_building=? and id_classroom=?";
                        SQLiteStatement stat_1 = db.compileStatement(sql_1);
                        stat_1.bindString(1,delete_name_building);
                        stat_1.bindString(2,delete_id_classroom);
                        stat_1.executeUpdateDelete();
                        stat_1.close();

                        //删除表classroom2中对应数据
                        String sql_2 = "delete from classroom2 " +
                                "where name_building=? and id_classroom=?";
                        SQLiteStatement stat_2 = db.compileStatement(sql_2);
                        stat_2.bindString(1,delete_name_building);
                        stat_2.bindString(2,delete_id_classroom);
                        stat_2.executeUpdateDelete();
                        stat_2.close();

                        //删除表classroom1中对应数据
                        String sql_3 = "delete from classroom3 " +
                                "where name_building=? and id_classroom=?";
                        SQLiteStatement stat_3 = db.compileStatement(sql_3);
                        stat_3.bindString(1,delete_name_building);
                        stat_3.bindString(2,delete_id_classroom);
                        stat_3.executeUpdateDelete();
                        stat_3.close();

                        db.setTransactionSuccessful();
                        db.endTransaction();

                        //更新数据
                        db_classroom_1 = get_db_classroom_infos(db,"classroom1","name_building");
                        db_classroom_2 = get_db_classroom_infos(db,"classroom2","name_building");
                        db_classroom_3 = get_db_classroom_infos(db,"classroom3","name_building");
                        Toast.makeText(AdminActivity.this,"删除教室成功",Toast.LENGTH_SHORT).show();
                    }catch (Exception e){
                        e.printStackTrace();
                        Log.d(Tag,"705行，添加新教室出现问题");
                        Toast.makeText(AdminActivity.this,"删除教室失败，请检查输入",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        //修改功能的各个控件
        EditText admin_modify_eT_name_building_old = findViewById(R.id.admin_modify_eT_name_building_old);
        EditText admin_modify_eT_id_classroom_old = findViewById(R.id.admin_modify_eT_id_classroom_old);

        EditText admin_modify_eT_name_building_new = findViewById(R.id.admin_modify_eT_name_building_new);
        EditText admin_modify_eT_id_classroom_new = findViewById(R.id.admin_modify_eT_id_classroom_new);

        //清空
        admin_modify_eT_name_building_old.setText("");
        admin_modify_eT_id_classroom_old.setText("");
        admin_modify_eT_name_building_new.setText("");
        admin_modify_eT_id_classroom_new.setText("");

        Button admin_modify_button = findViewById(R.id.admin_modify_button);

        admin_modify_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name_building_old = admin_modify_eT_name_building_old.getText().toString().trim();
                String id_classroom_old = admin_modify_eT_id_classroom_old.getText().toString().trim();

                String name_building_new = admin_modify_eT_name_building_new.getText().toString().trim();
                String id_classroom_new = admin_modify_eT_id_classroom_new.getText().toString().trim();

                if(name_building_old.equals("") || name_building_new.equals("")
                        || id_classroom_old.equals("") || id_classroom_new.equals("")){
                    Toast.makeText(AdminActivity.this,"输入不可为空",Toast.LENGTH_SHORT).show();
                }else {
                    try {

                        ContentValues values = new ContentValues();
                        values.put("name_building",name_building_new);
                        values.put("id_classroom",id_classroom_new);

                        //更新表classroom1
                        db.update("classroom1",values,"name_building=? and id_classroom=?",new String[] {name_building_old,id_classroom_old});
                        //更新表classroom2
                        db.update("classroom2",values,"name_building=? and id_classroom=?",new String[] {name_building_old,id_classroom_old});
                        //更新表classroom3
                        db.update("classroom3",values,"name_building=? and id_classroom=?",new String[] {name_building_old,id_classroom_old});

                        //更新数据
                        db_classroom_1 = get_db_classroom_infos(db,"classroom1","name_building");
                        db_classroom_2 = get_db_classroom_infos(db,"classroom2","name_building");
                        db_classroom_3 = get_db_classroom_infos(db,"classroom3","name_building");
                        Toast.makeText(AdminActivity.this,"修改教室成功",Toast.LENGTH_SHORT).show();
                    }catch (Exception e){
                        e.printStackTrace();
                        Log.d(Tag,"806行，修改教室出现问题");
                        Toast.makeText(AdminActivity.this,"修改教室失败，请检查输入",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    /*
        以下两个方法是关于本Activity使用的菜单。
        onCreateOptionsMenu指定本Activity使用的是admin_menu菜单。
        onOptionsItemSelected指定了点击菜单后的事件，即调转到对应页面。
        管理员页面的菜单设置可参考该段代码。
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.admin_menu,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        if(id == R.id.admin_menu_search){
            setSA();
        } else if (id == R.id.admin_menu_center) {
            setCenter();
        }else if (id == R.id.admin_menu_add_modify_delete){
            setAMD();
        }else {
            Toast.makeText(AdminActivity.this,"你是怎么做到的",Toast.LENGTH_SHORT).show();
        }
        return true;
    }
}